(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.industryDefault.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'industryDefaultService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, industryDefaultService) {
		var ctrl = this, industryDefaultInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.industryDefault = {};

		ctrl.init = function() {
			var params = {
					industryDefaultCode : industryDefaultInfo.industryCode
			};
			authService.create(params).then(function(response) {
				ctrl.industryDefault = response.data || industryDefaultInfo;
			});
		};
		
		ctrl.setDefIndustryCode = function (){
			if (ctrl.industryDefault && ctrl.industryDefault.sicCode != null){
				ctrl.industryDefault.siteCode = storageService.getUserInfo().siteCode;
				ctrl.industryDefault.industryCode = ctrl.industryDefault.siteCode + ":" + ctrl.industryDefault.sicCode;
			} else {
				if (ctrl.industryDefault)
				ctrl.industryDefault.industryCode = null;
			}
		};

		ctrl.createSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.industryDefault.taskId) {
				return authService.createContinueSave(ctrl.industryDefault).then(saveSuccessful, saveFailed);
			}  else {
				return authService.createSave(ctrl.industryDefault).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.industryDefault.taskId) {
				return authService.createContinueSubmit(ctrl.industryDefault).then(submitSuccessful, submitFailed);
			}  else {
				return authService.createSubmit(ctrl.industryDefault).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.industryDefault.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.industrydefault.list',
					'state' : 'cda.industryDefault.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.industryDefault = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);